<?php
/**
 * Configuration controller class for EICms
 * 
 * @package EICms
 * @author hcderaad
 */
/**
 * Configuration controller class for EICms
 * 
 * @package EICms
 * @author hcderaad
 */
class Admin_ConfigController extends EICms_Controller_Action {
	
	/**
	 * The ini values.
	 *
	 * @var array
	 */
	protected $iniValues;
	
	/**
	 * Returns the ini values.
	 *
	 * @return array
	 */
	protected function getIniValues() {
		if(empty($this->iniValues)) {
			$configFile = Zend_Registry::get('configFile');
			if(!file_exists($configFile) || !is_writable($configFile)) {
				throw new EICms_Exception('Configuration file is missing or not writable: ' . $configFile);
			}
			$this->iniValues = parse_ini_file($configFile,TRUE);
		}
		return $this->iniValues;
	}
		
	/**
	 * Shows the configuration data and provides edit links.
	 *
	 */
	public function indexAction() {
		$this->view->title = $this->_('config');
		$this->view->adminbar = '<a href="' . EICms_Url::get('index','index',array(),'admin') . '">' . $this->_('back') . '</a>' . PHP_EOL; 
		$this->view->chooseeditsection = $this->_('chooseeditsection');
		$iniValues = $this->getIniValues();
		$this->view->assign('sections',array_keys($iniValues));
		$this->view->assign('iniValues',$iniValues);
	}
	
	/**
	 * Edits a configuration section and stores changes if validated.
	 *
	 */
	public function editAction() {
		$this->view->title = $this->_('config');
		$section = $this->getRequest()->getParam('section');
		$this->view->section = ucFirst($section);
		$form = $this->getSectionEditForm($section);
		$iniValues = $this->getIniValues();
		$formElementValues = array();
		foreach($iniValues[$section] as $key => $value) {
			$key = str_replace('.','__',$key);
			$formElementValues[$key] = $value;
		}
		$form->populate($formElementValues);
		$this->view->form = $form->__toString();
		
	}
	
	/**
	 * Validates and saves the edited values to the configuration file.
	 *
	 */
	public function saveAction() {
		$this->view->title = $this->_('config');
		$section = $this->getRequest()->getParam('section');
		$this->view->section = ucFirst($section);
		$form = $this->getSectionEditForm($section);
		if($this->_request->isPost()) {
			$formData = $this->_request->getPost();
			if(!$form->isValid($formData)) {
				$this->view->form = $form->__toString();
			}
			else {
				$iniData = array();
				foreach($formData as $key => $value) {
					$key = str_replace('__','.',$key);
					$iniData[$key] = $value;
				}
				unset($iniData['submit']);
				$iniValues = $this->getIniValues();
				$iniValues[$section] = $iniData;
				if(EICms_IniWriter::write($iniValues,Zend_Registry::get('configFile'))) {
					$this->_forward('index');
				}
				else {
					$this->content = 'ERROR';
				}
			}
		}
	}
	
	/**
	 * Returns the editform
	 *
	 * @param string $section
	 * @return Zend_Dojo_Form
	 */
	protected function getSectionEditForm($section) {
		$form = new Zend_Dojo_Form();
		$form->setAction(EICms_Url::get('config','save',array('section' => $section),'admin'));
		$form->setMethod('post');
		$iniValues = $this->getIniValues();
		foreach($iniValues[$section] as $key => $val) {
			if(strpos($key,'.') !== FALSE) {
				$key = str_replace('.','__',$key);
			}
			$form->addElement('text',$key
								,array('label' => ucWords(str_replace(array('_','.'),' ',$key)),
										'required' => true,
										'size' => 50));
		}
		$form->addElement('submit','submit',array('label' => $this->_('save')));
		return $form;
	}
}
?>